#include <stdio.h>
#include <malloc.h>
#include <string.h>

#include "communication.h"
#include "debug.h"
#include "ioctl.h"

static int communication_login(char *data);
static int communication_register(char *data);
static int communication_add_friend(char *data);
static int communication_delete_friend(char *data);
static int communication_send_message(char *data);

/**
 * @brief   对顶层协议的包进行初始化
 * @param   
 * @return  struct communication_core的指针
 */
struct communication_core *communication_init(void)
{
    struct communication_core *communication_core = (struct communication_core *)
        malloc(sizeof(struct communication_core));

    communication_core->option = (struct communication_option *)
        malloc(sizeof(struct communication_option));

    communication_core->top_protocol[LOGIN_REQUEST].cmd_type[0] = REQUEST_ANSWER_BAG;
    communication_core->top_protocol[LOGIN_REQUEST].cmd[0] = CODE_LOGIN_REQUEST;
    memset(communication_core->top_protocol[LOGIN_REQUEST].data, 0,
        sizeof(communication_core->top_protocol[LOGIN_REQUEST].data));

    communication_core->top_protocol[LOGIN_ANSWER].cmd_type[0] = REQUEST_ANSWER_BAG;
    communication_core->top_protocol[LOGIN_ANSWER].cmd[0] = CODE_LOGIN_ANSWER;
    memset(communication_core->top_protocol[LOGIN_ANSWER].data, 0,
        sizeof(communication_core->top_protocol[LOGIN_ANSWER].data));

    communication_core->top_protocol[REGISTER_REQUEST].cmd_type[0] = REQUEST_ANSWER_BAG;
    communication_core->top_protocol[REGISTER_REQUEST].cmd[0] = CODE_REGISTER_REQUEST;
    memset(communication_core->top_protocol[REGISTER_REQUEST].data, 0,
        sizeof(communication_core->top_protocol[REGISTER_REQUEST].data));

    communication_core->top_protocol[REGISTER_ANSWER].cmd_type[0] = REQUEST_ANSWER_BAG;
    communication_core->top_protocol[REGISTER_ANSWER].cmd[0] = CODE_REGISTER_ANSWER;
    memset(communication_core->top_protocol[REGISTER_ANSWER].data, 0,
        sizeof(communication_core->top_protocol[REGISTER_ANSWER].data));

    communication_core->top_protocol[ADD_FRIEND_REQUEST].cmd_type[0] = REQUEST_ANSWER_BAG;
    communication_core->top_protocol[ADD_FRIEND_REQUEST].cmd[0] = CODE_ADD_FRIEND_REQUEST;
    memset(communication_core->top_protocol[ADD_FRIEND_REQUEST].data, 0,
        sizeof(communication_core->top_protocol[ADD_FRIEND_REQUEST].data));

    communication_core->top_protocol[ADD_FRIEND_ANSWER].cmd_type[0] = REQUEST_ANSWER_BAG;
    communication_core->top_protocol[ADD_FRIEND_ANSWER].cmd[0] = CODE_ADD_FRIEND_ANSWER;
    memset(communication_core->top_protocol[ADD_FRIEND_ANSWER].data, 0,
        sizeof(communication_core->top_protocol[ADD_FRIEND_ANSWER].data));

    communication_core->top_protocol[DELETE_FRIEND_REQUEST].cmd_type[0] = REQUEST_ANSWER_BAG;
    communication_core->top_protocol[DELETE_FRIEND_REQUEST].cmd[0] = CODE_DELETE_FRIEND_REQUEST;
    memset(communication_core->top_protocol[DELETE_FRIEND_REQUEST].data, 0,
        sizeof(communication_core->top_protocol[DELETE_FRIEND_REQUEST].data));

    communication_core->top_protocol[DELETE_FRIEND_ANSWER].cmd_type[0] = REQUEST_ANSWER_BAG;
    communication_core->top_protocol[DELETE_FRIEND_ANSWER].cmd[0] = CODE_DELETE_FRIEND_ANSWER;
    memset(communication_core->top_protocol[DELETE_FRIEND_ANSWER].data, 0,
        sizeof(communication_core->top_protocol[DELETE_FRIEND_ANSWER].data));

    communication_core->top_protocol[SEND_MESSAGE_REQUEST].cmd_type[0] = REQUEST_ANSWER_BAG;
    communication_core->top_protocol[SEND_MESSAGE_REQUEST].cmd[0] = CODE_SEND_MESSAGE_REQUEST;
    memset(communication_core->top_protocol[SEND_MESSAGE_REQUEST].data, 0,
        sizeof(communication_core->top_protocol[SEND_MESSAGE_REQUEST].data));

    communication_core->top_protocol[SEND_MESSAGE_ANSWER].cmd_type[0] = REQUEST_ANSWER_BAG;
    communication_core->top_protocol[SEND_MESSAGE_ANSWER].cmd[0] = CODE_SEND_MESSAGE_ANSWER;
    memset(communication_core->top_protocol[SEND_MESSAGE_ANSWER].data, 0,
        sizeof(communication_core->top_protocol[SEND_MESSAGE_ANSWER].data));

    
    communication_core->option->communication_login = communication_login;
    communication_core->option->communication_register = communication_register;
    communication_core->option->communication_add_friend = communication_add_friend;
    communication_core->option->communication_delete_friend = communication_delete_friend;
    communication_core->option->communication_send_message = communication_send_message;

    return communication_core;
}

/**
 * @brief   生成顶层包
 * @param   communication_core:通信协议的所有数据
 * @return  生成的顶层包
 */
char *communication_top_build(struct communication_core *communication_core)
{

}

/**
 * @brief   生成底层包
 * @param   communication_core:通信协议的所有数据
 * @return  生成的底层包
 */
char *communication_device_build(struct communication_core *communication_core)
{

}

/**
 * @brief   对接收的顶层包进行分析
 * @param   data:顶层协议的包
 * @return  数据包对应的指令cmd
 */
int communication_top_analysis(char *data)
{
    struct top_protocol *top_protocol_bag;
    char reguest_answer_bag = REQUEST_ANSWER_BAG;

    if (!data) {
        DEBUG("COMMUNICATION_TOP_ANALYSIS PARAM FALSE!\n");
        //return PARAM_FALSE;
    }

}

/**
 * @brief   对接收的底层包进行分析
 * @param   data:底层协议的包
 * @return  数据包中的有效数据
 */
char *communication_dev_analysis(char *data)
{

}

/**
 * @brief   登录的通信协议部分
 * @param   底层协议的数据包
 * @return  成功或失败的状态
 */
static int communication_login(char *data)
{
}

/**
 * @brief   注册的通信协议部分
 * @param   底层协议的数据包
 * @return  成功或失败的状态
 */
static int communication_register(char *data)
{

}

/**
 * @brief   添加好友的通信协议部分
 * @param   底层协议的数据包
 * @return  成功或失败的状态
 */
static int communication_add_friend(char *data)
{}

/**
 * @brief   删除好友的通信协议部分
 * @param   底层协议的数据包
 * @return  成功或失败的状态
 */
static int communication_delete_friend(char *data)
{}

/**
 * @brief   发送消息的通信协议部分
 * @param   底层协议的数据包
 * @return  成功或失败的状态
 */
static int communication_send_message(char *data)
{}